package com.yearEndWorkload.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yearEndWorkload.handler.excelHandler.NewOutstandingSheetWriteHandler;
import com.yearEndWorkload.handler.excelHandler.SpecialWorkSheetWriteHandler;
import com.yearEndWorkload.handler.excelHandler.WorkloadSheetWriteHandler;
import com.yearEndWorkload.listen.ExcelResProp;
import com.yearEndWorkload.pojo.entity.TypeStandard;
import com.yearEndWorkload.pojo.excel.importTemplate.*;
import com.yearEndWorkload.service.impl.TypeStandardServiceImpl;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;



@Slf4j
@RestController
@RequestMapping("/s3/templateExport")
@Tag(name = "导出XXX年的填写模板", description = "表格模板生成器")
public class TemplateController {

    @Autowired
    TypeStandardServiceImpl typeStandardService;

    @Operation(summary = "导出工作量导入表填写模板", description = "导出工作量导入表填写模板")
    @SneakyThrows(IOException.class)
    @GetMapping("/exportWorkloadImport/{year}")
    public void exportWorkloadImport(HttpServletResponse response, @PathVariable String year) {

        QueryWrapper<TypeStandard> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("year",year);
        List<TypeStandard> list = typeStandardService.list(queryWrapper);
        List<String> collect = list.stream().map(TypeStandard::getWorkloadType).collect(Collectors.toList());

        WorkloadSheetWriteHandler workloadSheetWriteHandler = new WorkloadSheetWriteHandler();
        workloadSheetWriteHandler.selectList(collect);
        ExcelResProp.setExcelRespProp(response, "导出工作量导入表填写模板");

        EasyExcel.write(response.getOutputStream(), WorkloadImportExcel.class)
                .registerWriteHandler(workloadSheetWriteHandler)
                .excelType(ExcelTypeEnum.XLSX)
                .sheet("工作量导入表填写模板")
                .doWrite(new ArrayList<WorkloadImportExcel>());
    }

    @Operation(summary = "导出管理服务绩效考核管理填写模板", description = "导出管理服务绩效考核管理填写模板")
    @SneakyThrows(IOException.class)
    @GetMapping("/exportServicePerformance/{year}")
    public void exportServicePerformance(HttpServletResponse response, @PathVariable String year) {

        ExcelResProp.setExcelRespProp(response, "导出管理服务绩效考核管理填写模板");

        EasyExcel.write(response.getOutputStream(), ServicePerformanceExcel.class)
                .excelType(ExcelTypeEnum.XLSX)
                .sheet("管理服务绩效考核管理填写模板")
                .doWrite(new ArrayList<ServicePerformanceExcel>());
    }


    @Operation(summary = "导出考核平均绩效填写模板", description = "导出考核平均绩效填写模板")
    @SneakyThrows(IOException.class)
    @GetMapping("/exportAssessAveragePerformance/{year}")
    public void exportAssessAveragePerformance(HttpServletResponse response, @PathVariable String year) {

        ExcelResProp.setExcelRespProp(response, "导出考核平均绩效填写模板");

        EasyExcel.write(response.getOutputStream(), AssessAveragePerformanceExcel.class)
                .excelType(ExcelTypeEnum.XLSX)
                .sheet("考核平均绩效填写模板")
                .doWrite(new ArrayList<AssessAveragePerformanceExcel>());
    }

    @Operation(summary = "导出新增突出业绩成果奖励管理填写模板", description = "导出新增突出业绩成果奖励管理填写模板")
    @SneakyThrows(IOException.class)
    @GetMapping("/exportNewOutstanding/{year}")
    public void exportNewOutstanding(HttpServletResponse response, @PathVariable String year) {

        QueryWrapper<TypeStandard> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("year",year);
        List<TypeStandard> list = typeStandardService.list(queryWrapper);
        List<String> collect = list.stream().map(TypeStandard::getWorkloadType).collect(Collectors.toList());

        NewOutstandingSheetWriteHandler newOutstandingSheetWriteHandler = new NewOutstandingSheetWriteHandler();
        newOutstandingSheetWriteHandler.selectList(collect);
        ExcelResProp.setExcelRespProp(response, "新增突出业绩成果奖励管理填写模板");

        EasyExcel.write(response.getOutputStream(), NewOutstandingExcel.class)
                .registerWriteHandler(newOutstandingSheetWriteHandler)
                .excelType(ExcelTypeEnum.XLSX)
                .sheet("新增突出业绩成果奖励管理填写模板")
                .doWrite(new ArrayList<NewOutstandingExcel>());
    }

    @Operation(summary = "导出专项工作绩效管理填写模板", description = "导出专项工作绩效管理填写模板")
    @SneakyThrows(IOException.class)
    @GetMapping("/exportSpecialWork/{year}")
    public void exportSpecialWork(HttpServletResponse response, @PathVariable String year) {

        QueryWrapper<TypeStandard> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("year",year);
        List<TypeStandard> list = typeStandardService.list(queryWrapper);
        List<String> collect = list.stream().map(TypeStandard::getWorkloadType).collect(Collectors.toList());

        SpecialWorkSheetWriteHandler specialWorkSheetWriteHandler = new SpecialWorkSheetWriteHandler();
        specialWorkSheetWriteHandler.selectList(collect);
        ExcelResProp.setExcelRespProp(response, "导出专项工作绩效管理填写模板");

        EasyExcel.write(response.getOutputStream(), SpecialWorkExcel.class)
                .registerWriteHandler(specialWorkSheetWriteHandler)
                .excelType(ExcelTypeEnum.XLSX)
                .sheet("专项工作绩效管理填写模板")
                .doWrite(new ArrayList<SpecialWorkExcel>());
    }

}
